<!--
 * @Version: 1.0
 * @Date: 2021-04-25 09:46:20
 * @LastEditTime: 2021-06-15 17:23:44
 * @Description:
-->
<template>
  <div>
    <el-card class="app-container">
      <div>
        <el-form :model="formData" inline label-width="150px">
          <div class="header_title_box">
            <h2 style="margin-right:50px">
              基本信息
            </h2>
          </div>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="付款单号：">
                <span>{{ formData.payment_no }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="货代公司：">
                <span>{{ formData.lforwarder_company }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="应付金额(USD)：">
                <span>{{ formData.order_amount }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="实际付款日期：">
                <span>{{ formData.pay_time ? formData.pay_time : '-' }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="实际付款金额：">
                <span>{{ formData.pay_amount !== '0' ? formData.pay_amount : '-' }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核状态：">
                <span>{{ statusData[formData.audit_status+''] }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="申请状态：">
                <span>{{ formData.pay_status === 0 ? '待付款' : '已付款' }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div>
          <div class="header_title_box">
            <h2 style="margin-right:50px">
              付款单明细
            </h2>
            <div v-if="isEdit">
              <el-button v-if="selectedIdsFather.length>0" type="danger" size="mini" icon="el-icon-delete" @click="delField">批量删除</el-button>
            </div>
          </div>
          <div>
            <el-table :data="transportList" tooltip-effect="dark" style="width: 100%" border fit @selection-change="handleSelectMultiFather">
              <el-table-column v-if="isEdit" align="center" type="selection" width="55" />
              <el-table-column v-if="isEdit" align="center" label="操作" width="70" fixed>
                <template slot-scope="sales">
                  <span class="disable_style" @click="deleteField(sales.row)">删除</span>
                </template>
              </el-table-column>
              <el-table-column align="center" label="提单号" prop="bl_no" width="170" />
              <el-table-column align="center" label="柜号" prop="cabinet_no" width="150" />
              <el-table-column align="center" label="装柜日期" prop="loading_date" width="150" />
              <el-table-column align="center" label="起运日期" prop="shipment_date" width="150" />
              <el-table-column align="center" label="预计到仓日期" prop="arrive_date" width="150" />
              <el-table-column align="center" label="所属货代公司" prop="lforwarder_company" />
              <el-table-column align="center" label="货代公司结算周期(天)" prop="settlement_cycle" width="150" />
              <el-table-column align="center" label="付款单号" prop="payment_no" width="150" />
              <el-table-column align="center" label="申请付款状态" prop="pay_status">
                <template slot-scope="scope">
                  {{ scope.row.pay_status===1 ? '已申请': '待申请' }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="国内路运费用" prop="domestic_fee" />
              <el-table-column align="center" label="海运费用" prop="ocean_fee" width="150" />
              <el-table-column align="center" label="国外路运费用" prop="overseas_fee" width="160" />
              <el-table-column align="center" label="应付金额" prop="total_fee">
                <template slot-scope="scope">
                  {{ accAddFuc(accAddFuc(scope.row.ocean_fee, scope.row.overseas_fee ), scope.row.domestic_fee) }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="预计付款时间" prop="estimated_pay_time" width="150" />
            </el-table>
          </div>
        </div>
        <div class="footer-center">
          <el-button v-if="isEdit" type="primary" @click="handleSubmitNew">更 新</el-button>
          <el-button @click="handleCancelPath">{{ isEdit ? '取 消' : '返 回' }}</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { accAdd } from '@/utils/index.js'
export default {
  data() {
    return {
      loadingInfo: false,
      formData: {},
      id: '',
      selectedIdsFather: [],
      transportList: [],
      title: '',
      statusData: {
        '0': '待审核',
        '1': '审核通过',
        '-1': '审核拒绝'
      }
    }
  },
  computed: {
    isEdit() {
      return this.title.includes('编辑')
    }
  },
  created() {
    this.loadingInfo = true
    this.id = this.$route.params.id
    this.title = this.$route.query.title
    this.formData = JSON.parse(this.$route.query.formData)
    this.orderInfo()
  },
  methods: {
    accAddFuc(val1, val2) {
      return accAdd(val1, val2)
    },
    handleSubmitNew() {
      const data = {}
      data.bill_ids = this.transportList.map((item) => {
        return item.id
      })
      data.id = this.id
      this.$http.post(`/freight-bill-orders/freight-order-update`, data).then((res) => {
        if (res.code === 10000) {
          this.$message.success('更新成功')
          this.handleCancelPath()
        }
      })
    },
    handleCancelPath() {
      this.$router.go(-1)
    },
    orderInfo() {
      this.$http.get(`/freight-bill?payment_no=${this.formData.payment_no}`, { params: { limit: 1000, page: 1 }}).then((res) => {
        if (res.code === 10000) {
          const dataArr = res.data
          this.transportList = dataArr
          this.loadingInfo = false
        }
      }).catch(() => {
        this.loadingInfo = false
      })
    },
    deleteField(row) {
      const vm = this
      vm.transportList.splice(vm.transportList.indexOf(row), 1)
    },
    // 批量删除
    delField() {
      const vm = this
      const dataArr = [...this.transportList]
      dataArr.map((arr) => {
        if (vm.selectedIdsFather.includes(arr)) {
          vm.transportList.splice(vm.transportList.indexOf(arr), 1)
        }
      })
    },
    // 选择
    handleSelectMultiFather(data) {
      const vm = this
      vm.selectedIdsFather = []
      data.forEach(item => {
        vm.selectedIdsFather.push(item)
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
